<template>
<div class="page-container-padding">
  <div style="display:flex;align-items:center;">
    <el-select v-model="currentUserId" size="mini">
      <el-option v-for="user in userList" :value="user.id" :label="user.admin_name" :key="user.id"></el-option>
    </el-select>
    <span style="margin-left:6px;"><el-button type="primary" size="mini" @click="toQueryAuthVariety">查询</el-button></span>
    <span style="margin-left:6px;"><el-input v-model="currentVarietyEn" size="mini" placeholder="录入品种代码添加"></el-input></span>
    <span style="margin-left:6px;"><el-button size="mini" type="primary" @click="toSaveUserVariety">添加</el-button></span>
  </div>
  <div style="margin-top:10px;">
    <div v-if="authVarietyList.length>0">
      <el-table border stripe :data="authVarietyList">
        <el-table-column prop="admin_name" label="姓名" width="110px"></el-table-column>
        <el-table-column prop="variety_name" label="品种" width="110px"></el-table-column>
        <el-table-column prop="variety_en" label="代码" width="110px"></el-table-column>
        <el-table-column prop="start_date" label="开始日期" width="110px"></el-table-column>
        <el-table-column prop="end_date" label="结束日期" width="110px"></el-table-column>
        <el-table-column prop="active" label="状态" width="70px" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.active" @change="activeChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-else>
      <el-empty description="该人员还没设置品种"></el-empty>
    </div>
  </div>
</div>
</template>

<script>
import { getYesterday,datetimeToStr } from '@/utils/datetimeUtils'
import { getAnalystList } from '@/api/user'
import { getUserVariety, saveUserVariety } from '@/api/departmentWork/workSetting'
export default {
  data(){
    return {
      currentUserId: null,
      userList: [],
      currentVarietyEn: '',

      authVarietyList: []
    }
  },
  mounted(){
    getAnalystList().then(res => this.userList = res.data)

  },
  methods:{
    toQueryAuthVariety(){
      getUserVariety({user: this.currentUserId}).then(res => {
        this.authVarietyList = res.data
      })
    },

    // 保存用户品种任务
    toSaveUserVariety(){
      const data={
        user_id: this.currentUserId,
        variety_en: this.currentVarietyEn
      }
      if (!data.user_id || !data.variety_en){
        this.$baseMessage('请选择或填写正确数据！', 'error')
        return
      }
      saveUserVariety(data).then(res => {
        this.$baseMessage(res.msg, res.code===200?'success':'error')
        this.currentVarietyEn = null
        this.toQueryAuthVariety()
      })
    },

    // 有效改变
    activeChange(row){
      const data = {
        user_id: this.currentUserId,
        variety_en: row.variety_en,
        end_date: getYesterday(true,true,'-')
      }
      if (row.active){
        data.end_date = '2099-12-31'
      }
      saveUserVariety(data).then(res => {
        this.$baseMessage(res.msg, res.code===200?'success':'error')
        this.toQueryAuthVariety()
      })
    }
  }
}
</script>

<style scoped>
</style>
